import pprint
import sys
from oauth2client import client
from apiclient import sample_tools

def main(argv):
  service, flags = sample_tools.init(
      argv, 'urlshortener', 'v1', __doc__, __file__,
      scope='https://www.googleapis.com/auth/urlshortener')

      
  try:
    urlshort = str(sys.argv[1])
    print urlshort# --Usado como mecanismo de control.
    print argv
    url = service.url()


    # Create a shortened URL by inserting the URL into the url collection.
    body = {'longUrl': urlshort }
    resp = url.insert(body=body).execute()
    #pprint.pprint(resp)

    short_url = resp['id']
    print "Short URL: ", short_url
    # Convert the shortened URL back into a long URL
  #  resp = url.get(shortUrl=short_url).execute()
   # pprint.pprint(resp)

  except client.AccessTokenRefreshError:
    print ('The credentials have been revoked or expired, please re-run'
      'the application to re-authorize')

if __name__ == '__main__':
#Hacer algo para que, al pasar mal los args salte un usage:
 try:
  main(sys.argv[1:])
 except Exception:
  print "Usage: python GoGoGoo.py URL-TO-SHORTEN"
  print "Example: python GoGoGoo.py http://eldritch.com.ar"